package com.doordu.framework.cloud.xxl.registry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author zxw
 * @date 2020/9/21 14:20
 */
public class DoorduJobAutoRegistration implements AutoServiceRegistration, SmartLifecycle {

    private static final Log log = LogFactory.getLog(DoorduJobAutoRegistration.class);

    private ApplicationContext applicationContext;
    private DoorduJobRegistry doorduJobRegistry;
    private DoorduJobRegistration doorduJobRegistration;
    private AtomicBoolean running = new AtomicBoolean(false);

    public DoorduJobAutoRegistration(ApplicationContext applicationContext, DoorduJobRegistry doorduJobRegistry, DoorduJobRegistration doorduJobRegistration) {
        this.applicationContext = applicationContext;
        this.doorduJobRegistry = doorduJobRegistry;
        this.doorduJobRegistration = doorduJobRegistration;
    }


    @Override
    public void start() {
        this.doorduJobRegistration.injectJobMenthod(applicationContext);
        doorduJobRegistry.register(this.doorduJobRegistration);
        this.running.set(true);
    }

    @Override
    public void stop() {
        doorduJobRegistry.deregister(this.doorduJobRegistration);
        this.running.set(false);
    }

    @Override
    public boolean isRunning() {
        return this.running.get();
    }
}
